using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qGAAUF = from m in mtemp.GAAUF select m;
       DBMapper.MapGAAUF(qGAAUF.ToList(), true);
       */
        public List<GAAUF> MapGAAUF(XElement item2, bool mergedb)
        {
            List<GAAUF> result = new List<GAAUF>();
            var item4 = from item3 in item2.Elements()
                        select new GAAUF
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            GAMANDANT = (string)item3.Element("GAMANDANT"),
                            GAAUFKRS = (string)item3.Element("GAAUFKRS"),
                            GAAUFLFD = (string)item3.Element("GAAUFLFD"),
                            GAAUFPOS = (string)item3.Element("GAAUFPOS"),
                            GASA = (string)item3.Element("GASA"),
                            GAPOS = (string)item3.Element("GAPOS"),
                            GAART = (string)item3.Element("GAART"),
                            GAVAR = (string)item3.Element("GAVAR"),
                            GAFARBE = (string)item3.Element("GAFARBE"),
                            GAKG1 = (string)item3.Element("GAKG1"),
                            GAKG2 = (string)item3.Element("GAKG2"),
                            GAKG3 = (string)item3.Element("GAKG3"),
                            GAKG4 = (string)item3.Element("GAKG4"),
                            GAKG5 = (string)item3.Element("GAKG5"),
                            GAKG6 = (string)item3.Element("GAKG6"),
                            GAKG7 = (string)item3.Element("GAKG7"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapGAAUF(result, mergedb);
          return result;
        }
        ///DBMapper.MapGAAUF(mtemp.GAAUF.ToList<GAAUF>(), true);        
       public void MapGAAUF(List<GAAUF> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from GAAUF row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.GAAUF.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.GAAUF.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.GAAUF.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "GAAUF")]
    public class GAAUF : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _GAMANDANT="0";
        [Column]
        public string GAMANDANT
        {
            get
            {
                return _GAMANDANT;
            }
            set
            {
                if (value != _GAMANDANT)
                {
                     _GAMANDANT = value;
                    NotifyPropertyChanged("GAMANDANT");
                }
            }
        }
        private string  _GAAUFKRS="0";
        [Column]
        public string GAAUFKRS
        {
            get
            {
                return _GAAUFKRS;
            }
            set
            {
                if (value != _GAAUFKRS)
                {
                     _GAAUFKRS = value;
                    NotifyPropertyChanged("GAAUFKRS");
                }
            }
        }
        private string  _GAAUFLFD="0";
        [Column]
        public string GAAUFLFD
        {
            get
            {
                return _GAAUFLFD;
            }
            set
            {
                if (value != _GAAUFLFD)
                {
                     _GAAUFLFD = value;
                    NotifyPropertyChanged("GAAUFLFD");
                }
            }
        }
        private string  _GAAUFPOS="0";
        [Column]
        public string GAAUFPOS
        {
            get
            {
                return _GAAUFPOS;
            }
            set
            {
                if (value != _GAAUFPOS)
                {
                     _GAAUFPOS = value;
                    NotifyPropertyChanged("GAAUFPOS");
                }
            }
        }
        private string  _GASA="0";
        [Column]
        public string GASA
        {
            get
            {
                return _GASA;
            }
            set
            {
                if (value != _GASA)
                {
                     _GASA = value;
                    NotifyPropertyChanged("GASA");
                }
            }
        }
        private string  _GAPOS="0";
        [Column]
        public string GAPOS
        {
            get
            {
                return _GAPOS;
            }
            set
            {
                if (value != _GAPOS)
                {
                     _GAPOS = value;
                    NotifyPropertyChanged("GAPOS");
                }
            }
        }
        private string  _GAART="0";
        [Column]
        public string GAART
        {
            get
            {
                return _GAART;
            }
            set
            {
                if (value != _GAART)
                {
                     _GAART = value;
                    NotifyPropertyChanged("GAART");
                }
            }
        }
        private string  _GAVAR="0";
        [Column]
        public string GAVAR
        {
            get
            {
                return _GAVAR;
            }
            set
            {
                if (value != _GAVAR)
                {
                     _GAVAR = value;
                    NotifyPropertyChanged("GAVAR");
                }
            }
        }
        private string  _GAFARBE="0";
        [Column]
        public string GAFARBE
        {
            get
            {
                return _GAFARBE;
            }
            set
            {
                if (value != _GAFARBE)
                {
                     _GAFARBE = value;
                    NotifyPropertyChanged("GAFARBE");
                }
            }
        }
        private string  _GAKG1="0";
        [Column]
        public string GAKG1
        {
            get
            {
                return _GAKG1;
            }
            set
            {
                if (value != _GAKG1)
                {
                     _GAKG1 = value;
                    NotifyPropertyChanged("GAKG1");
                }
            }
        }
        private string  _GAKG2="0";
        [Column]
        public string GAKG2
        {
            get
            {
                return _GAKG2;
            }
            set
            {
                if (value != _GAKG2)
                {
                     _GAKG2 = value;
                    NotifyPropertyChanged("GAKG2");
                }
            }
        }
        private string  _GAKG3="0";
        [Column]
        public string GAKG3
        {
            get
            {
                return _GAKG3;
            }
            set
            {
                if (value != _GAKG3)
                {
                     _GAKG3 = value;
                    NotifyPropertyChanged("GAKG3");
                }
            }
        }
        private string  _GAKG4="0";
        [Column]
        public string GAKG4
        {
            get
            {
                return _GAKG4;
            }
            set
            {
                if (value != _GAKG4)
                {
                     _GAKG4 = value;
                    NotifyPropertyChanged("GAKG4");
                }
            }
        }
        private string  _GAKG5="0";
        [Column]
        public string GAKG5
        {
            get
            {
                return _GAKG5;
            }
            set
            {
                if (value != _GAKG5)
                {
                     _GAKG5 = value;
                    NotifyPropertyChanged("GAKG5");
                }
            }
        }
        private string  _GAKG6="0";
        [Column]
        public string GAKG6
        {
            get
            {
                return _GAKG6;
            }
            set
            {
                if (value != _GAKG6)
                {
                     _GAKG6 = value;
                    NotifyPropertyChanged("GAKG6");
                }
            }
        }
        private string  _GAKG7="0";
        [Column]
        public string GAKG7
        {
            get
            {
                return _GAKG7;
            }
            set
            {
                if (value != _GAKG7)
                {
                     _GAKG7 = value;
                    NotifyPropertyChanged("GAKG7");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<GAAUF> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\GAAUF\\GAAUF" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\GAAUF" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("GAAUF");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (GAAUF row in ListInput)
                {
                    XElement f = new XElement("GAAUF");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement GAMANDANT = new XElement("GAMANDANT");
                         GAMANDANT.Value = row.GAMANDANT;
                          f.Add(GAMANDANT);
                        XElement GAAUFKRS = new XElement("GAAUFKRS");
                         GAAUFKRS.Value = row.GAAUFKRS;
                          f.Add(GAAUFKRS);
                        XElement GAAUFLFD = new XElement("GAAUFLFD");
                         GAAUFLFD.Value = row.GAAUFLFD;
                          f.Add(GAAUFLFD);
                        XElement GAAUFPOS = new XElement("GAAUFPOS");
                         GAAUFPOS.Value = row.GAAUFPOS;
                          f.Add(GAAUFPOS);
                        XElement GASA = new XElement("GASA");
                         GASA.Value = row.GASA;
                          f.Add(GASA);
                        XElement GAPOS = new XElement("GAPOS");
                         GAPOS.Value = row.GAPOS;
                          f.Add(GAPOS);
                        XElement GAART = new XElement("GAART");
                         GAART.Value = row.GAART;
                          f.Add(GAART);
                        XElement GAVAR = new XElement("GAVAR");
                         GAVAR.Value = row.GAVAR;
                          f.Add(GAVAR);
                        XElement GAFARBE = new XElement("GAFARBE");
                         GAFARBE.Value = row.GAFARBE;
                          f.Add(GAFARBE);
                        XElement GAKG1 = new XElement("GAKG1");
                         GAKG1.Value = row.GAKG1;
                          f.Add(GAKG1);
                        XElement GAKG2 = new XElement("GAKG2");
                         GAKG2.Value = row.GAKG2;
                          f.Add(GAKG2);
                        XElement GAKG3 = new XElement("GAKG3");
                         GAKG3.Value = row.GAKG3;
                          f.Add(GAKG3);
                        XElement GAKG4 = new XElement("GAKG4");
                         GAKG4.Value = row.GAKG4;
                          f.Add(GAKG4);
                        XElement GAKG5 = new XElement("GAKG5");
                         GAKG5.Value = row.GAKG5;
                          f.Add(GAKG5);
                        XElement GAKG6 = new XElement("GAKG6");
                         GAKG6.Value = row.GAKG6;
                          f.Add(GAKG6);
                        XElement GAKG7 = new XElement("GAKG7");
                         GAKG7.Value = row.GAKG7;
                          f.Add(GAKG7);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
